#!user/bin/python
# -*- coding:UTF-8 -*-

import mysql.connector
import requests
from bs4 import BeautifulSoup


class SearchTopCrawler:
    def __init__(self, mysqlconfig):
        self.mysqlconfig = mysqlconfig
        self.mysqlconfig = {
            'user': 'root',
            'password': 'yzdpf19971015',
            'host': '127.0.0.1',
            'port': '3306',
            'database': 'javaweb',
            'charset': 'utf8'
        }

    def initDataBase(self):
        conn = mysql.connector.connect(**self.mysqlconfig)
        cursor = conn.cursor()
        creat_sql = "create table timeHot" \
                    "(" \
                        "rank        int(4)       not null primary key," \
                        "href        varchar(250) null," \
                        "content     varchar(120) null," \
                        "searchLevel varchar(20)  null" \
                    ");"
        cursor.execute(creat_sql)
        conn.commit()
        init_sql = "INSERT INTO timeHot (rank) VALUES (%s);"
        for i in range(1, 51):
            val = (i,)
            cursor.execute(init_sql, val)
        conn.commit()
        cursor.close()
        conn.close()

    def updataInfo(self):
        url = "http://top.baidu.com/buzz?b=1&fr=topindex"
        headers = {
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,'
                      'image/webp,image/apng,*/*;q=0.8',
            'Pragma': 'no-cache',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                          'AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/70.0.3538.77 Safari/537.36'
        }
        text = requests.get(url, headers=headers).content.decode("gb2312", "ignore")
        bs = BeautifulSoup(text, "lxml")
        list_table = bs.find(class_="list-table")
        list_tr = list_table.find_all("tr")
        timeHot = []
        for tr in range(1, len(list_tr)):
            oneTip = dict()
            item = list_tr[tr]
            if item.find(class_="first") is not None:
                oneTip["rank"] = int(item.find(class_="first").find("span").text)
                oneTip["href"] = item.find(class_="list-title")["href"]
                oneTip["content"] = item.find(class_="list-title").text
                oneTip["searchLevel"] = item.find(class_="last").text
                print(oneTip)
                timeHot.append(oneTip)
        print(timeHot)
        conn = mysql.connector.connect(**self.mysqlconfig)
        cursor = conn.cursor()
        updatesql = "UPDATE timeHot SET href=%s,content=%s,searchLevel=%s " \
                    "WHERE rank=%s;"
        for item in timeHot:
            val = (item["href"], item["content"], item["searchLevel"],
                   item["rank"])
            print(val)
            cursor.execute(updatesql, val)
        conn.commit()
        cursor.close()
        conn.close()


if __name__ == '__main__':
    searchTopCrawler = SearchTopCrawler("1")
    searchTopCrawler.updataInfo()
